package com.neu.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.neu.entity.User;
import com.neu.service.UserService;
import com.neu.util.JWTUtil;
import com.neu.util.JsonResult;
import com.neu.util.ResultCode;



@CrossOrigin
@Controller("userController")
@RequestMapping("/his/user")
public class UserController {
	/**
	 * 定义状态码200常量
	 */
	public static final int OK = 200;
	@Autowired
	private UserService userService;
	
	@ResponseBody
	@RequestMapping(value="/login", method = RequestMethod.POST)
	public JsonResult<User> login(@RequestBody String requestBody)  {
		com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(requestBody);
		String UserName = (String) jsonObject.get("username");
        String Password = (String) jsonObject.get("password");
        String token = JWTUtil.getJwtToken(UserName, Password);
		//实现登录接口，返回值为单个用户对象
		User user = userService.getUserBean(UserName, Password);
		
		JsonResult<User> result = new JsonResult<>();
		result.setData(user);
		if(user == null) {
			result.setMessage("登录失败，没有找到用户");
			result.setState(4000);
		}
		result.setState(OK);
		result.setToken(token);
		result.setMessage("登录成功	");
		return result;
	}
	
	@ResponseBody
	@RequestMapping("/get_user")
	public JsonResult<List<User>> findUser(@RequestParam("pageNum") int page,
			@RequestParam("pageSize") int count,
			@RequestParam("keywords") String keywords,HttpServletRequest request){
			JWTUtil.checkToken(request);
		List<User> list = userService.getUserList(page, count, keywords);
		Integer count1 = userService.getUserCount(keywords);
		
		return new JsonResult<List<User>>(ResultCode.SUCCESS,list,count1);
	}
	
	@ResponseBody
	@RequestMapping("/get_user_more")
	public JsonResult<List<User>> getUserList02(
			@Param("page") Integer page, 
		@Param("count") Integer count, 
		@Param("keywords") String keywords, 
		@Param("keywords02") String keywords02,
		@Param("keywords03") String keywords03){
		List<User> list = userService.getUserList02(page, count, keywords, keywords02, keywords03);
		return new JsonResult<List<User>>(ResultCode.SUCCESS,list);
	}
	
	@ResponseBody
	@RequestMapping(value="/add_user",method=RequestMethod.POST)
	public JsonResult<Integer> add(User user){
		int rows = userService.addUser(user);
		if(rows != 1) {
			return new JsonResult<Integer>(ResultCode.INSERT_ERROR,rows);
		}
		return new JsonResult<Integer>(ResultCode.SUCCESS,rows);
	}
	
	@ResponseBody 
	@RequestMapping("/getcount")
	public JsonResult<Integer> getUserCount(@Param("keywords") String keywords){
		int count = userService.getUserCount(keywords);
		return new JsonResult<Integer>(ResultCode.SUCCESS,count);
	}
	
	
	@ResponseBody
	@RequestMapping("/delete_user")
	public JsonResult<Integer> deleteUserById(@Param("id") int id){
		int rows = userService.deleteUserById(id);
		if(rows != 1){
			return new JsonResult<Integer>(ResultCode.DEL_ERROR,rows);
		}
		return new JsonResult<Integer>(ResultCode.SUCCESS,rows);
	}
	
	
	@ResponseBody
	@RequestMapping("/delete_user_more")
	public JsonResult<Integer> deleteUserByIds(@RequestBody Integer[] ids){
		Integer rows = userService.deleteUserByIds(ids);
		return new JsonResult<Integer>(ResultCode.SUCCESS,rows);
	}
}
